<template>
  <div class="container">
    <div class="fixed-box" :class="{'fixed-box-fixed': tabsRow}">
      <div class="btn-box flex">
        <div @click="cancel">取消</div>
        <div>
          <button @click="saveVeekray">完成<template v-if="labels.length !== 0">({{ labels.length }})</template></button>
        </div>
      </div>
    </div>
    <div class="customer-title">
      <div>标签<template v-if="labels.length !== 0">（{{ labels.length }}）</template></div>
    </div>
    <div class="tag-data pad-l-15 pad-r-15 bg-color-white flex v-align-c">
      <div class="flex-item sel-bar">
        <label v-for="(item, index) in labels" :key="index"
          @click="deleteTag(item)" :class="item.show ? 'active' : ''"
          class="tag-style flex-item pos-rel">
          <span v-if="item.show"
            @click="deleteLabel(item)">删除</span>
          {{item.labelName}}
        </label>
        <input class="tag-add flex-item" :class="labels.length !== 0 ? 'border-l' : ''"
          @confirm="addLabel(labelName)" v-model="labelName" placeholder="输入标签"/>
      </div>
    </div>
    <div class="customer-title mar-b--11">
      <div>所有标签</div>
    </div>
    <div class="tag-data pad-l-15 pad-r-15 flex v-align-c">
      <div class="flex-item">
        <label v-for="(item, index) in listData" :key="index"
          :class="item.show ? 'on' : ''" @click="selectItem(item)"
          class="tag-style flex-item fail">{{ item.labelName }}</label>
      </div>
    </div>

  </div>
</template>
<script>
  import {
    getMemberInfo, saveMemberDTO
  } from '@/api/customer'
  import { tagsGroupPaging } from '@/api/personal'
  export default {
    data() {
      return {
        labelName: '',
        labels: [],
        friendId: '',
        veekrayInfo: {},
        listData: [],
        listQuery: {
          memberId: '',
          currentPage: 1,
          size: 1000
        },
        showSaveDialog: false,
        tabsRow: false
      }
    },
    onPageScroll: function(e) {
      const query = this.$pt.createSelectorQuery()
      query.select('.ai-add').boundingClientRect()
      // query.selectViewport().scrollOffset()
      query.exec(res => {
        if (res[0].top <= -10) {
          this.tabsRow = true
        } else {
          this.tabsRow = false
        }
      })
    },
    mounted() {
      this.labels = []
      this.friendId = this.getCurrentPage().query.memberId
      if (this.friendId) {
        this.getVeekrayInfo()
      }
    },
    methods: {
      cancel() {
        const that = this
        this.$pt.showModal({
          content: '保存本次编辑？',
          success(res) {
            if (res.confirm) {
              that.saveVeekray()
            } else if (res.cancel) {
              console.log('用户取消')
            }
          }
        })
      },
      changeData() {
        this.listData.forEach(itemList => {
          itemList.show = false
          this.labels.forEach(itemLabel => {
            if (itemLabel.labelName === itemList.labelName) {
              itemList.show = true
            }
          })
        })
      },
      pageInit() {
        this.listData = []
        this.listQuery.memberId = this.getCustomerId
        tagsGroupPaging(this.listQuery).then(response => {
          if (response.isError) return
          response.data.records.forEach(element => {
            this.listData.push(element)
            this.changeData()
          })
        }).catch({})
      },
      selectItem(labelItem) {
        if (labelItem.show) {
          this.labels.forEach((item, index) => {
            if (labelItem.labelName === item.labelName) {
              this.labels.splice(index, 1)
            }
          })
          labelItem.show = false
        } else {
          console.log(111)
          this.addLabel(labelItem.labelName, labelItem)
        }
      },
      getVeekrayInfo() {
        getMemberInfo({
          memberId: this.getCustomerId,
          customerMemberId: this.friendId
        }).then((response) => {
          if (response.isError) return
          this.veekrayInfo = response.data
          if (this.veekrayInfo.veekrayLabelList) {
            for (var b = 0; b < this.veekrayInfo.veekrayLabelList.length; b++) {
              if (this.veekrayInfo.veekrayLabelList[b].labelGroupId) {
                this.labels.push({'labelName': this.veekrayInfo.veekrayLabelList[b].labelName,
                  'labelGroupId': this.veekrayInfo.veekrayLabelList[b].labelGroupId,
                  'memberId': this.veekrayInfo.veekrayLabelList[b].memberId,
                  'show': false,
                  'userId': this.veekrayInfo.veekrayLabelList[b].userId})
              } else {
                this.labels.push({'labelName': this.veekrayInfo.veekrayLabelList[b].labelName,
                  'show': false,
                  'memberId': this.getCustomerId,
                  'labelGroupId': ''})
              }
            }
          }
          this.pageInit()
        }).catch(() => {
        })
      },
      addLabel(labelName, labelItem) {
        let labels = []
        if (this.labels.length !== 0) {
          this.labels.forEach(item => {
            labels.push(item.labelName)
          })
        }
        if (labelName) {
          if (!labels.includes(labelName)) {
            if (labelItem) {
              this.labels.push({'labelName': labelItem.labelName,
                'labelGroupId': labelItem.labelGroupId,
                'memberId': labelItem.memberId,
                'show': false,
                'userId': labelItem.userId})
            } else {
              this.labels.push({'labelName': labelName, 'show': false, 'labelGroupId': '', 'memberId': this.getCustomerId})
            }
            this.changeData()
          } else {
            this.$utils.showToast({ title: '请输入不重复的标签！' })
          }
          this.labelName = ''
        }
      },
      deleteTag(label) {
        label.show = !label.show
      },
      deleteLabel(label) {
        this.labels.splice(this.labels.findIndex(item => item === label), 1)
        this.changeData()
      },
      saveVeekray() {
        console.log(this.veekrayInfo.remarkName)
        let data = {
          memberId: this.getCustomerId,
          customerMemberId: this.friendId, // 被访问的微客id
          veekrayLabelList: this.labels
        }
        if (this.veekrayInfo.remarkName) {
          data.remarkName = this.veekrayInfo.remarkName
        }
        if (this.veekrayInfo.memberRemarkId) {
          data.memberRemarkId = this.veekrayInfo.memberRemarkId
        }
        saveMemberDTO(data).then((response) => {
          if (response.isError) return
          this.$utils.showToast({ title: '保存成功' })
          setTimeout(() => {
            this.$pt.navigateBack({ delta: -1 })
          }, 500)
        }).catch(() => {
        })
      },
      getUserInfo(event) {
        console.log(event.detail)
      },
      onCloseDialog() {
        this.showSaveDialog = false
      }
    }
  }
</script>
<style lang="less" scoped>
  .container {
    background: #f5f5f5;
    min-height: calc(100vh);
  }
  .fixed-box {
    // position: fixed;
    top: 0;
    left: 0;
    height: 50px;
    width: 100%;
    border-top: 1px solid #ededed;
    background: white;
    z-index: 99;
    .btn-box {
      padding: 0 15px;
      height: 49px;
      font-size: 14px;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #ededed;
      button {
        background: #f8c400;
        line-height: 1;
        font-size: 14px;
        padding: 6px 5px;
        border-radius: 3px;
        color: #333333;
        &::after {
          border: unset;
        }
      }
    }
    .header-box {
      padding: 0 15px;
      justify-content: left;
      align-items: center;
      height: 49px;
      border-bottom: 1px solid #ededed;
      /deep/ .van-search {
        background: white !important;
        padding: 0px;
        // min-width: 80px;
      }
      /deep/ .van-cell--borderless {
        padding: 3px 10px 3px 0px !important;
      }
      /deep/ .van-field__body {
        margin-left: 10px;
      }
      .headshop-box {
        max-width: 240px;
        display: inline-block;
        .img-row {
          overflow-x: auto;
          .img-box {
            &:first-child {
              img {
                margin-left: 0px;
              }
            }
            img {
              // flex-basis: 35px;
              flex-shrink: 0;
              width: 35px;
              height: 35px;
              margin-left: 10px;
            }
          }
        }
      }
      .search-box {
        display: inline-block;
        width: 100%;
      }
    }
  }
  .fixed-box-fixed {
    position: fixed;
    top: 0;
    left: 0;
    box-shadow: 0px 2px 6px #c5c5c5;
  }
  .sel-bar{
    min-height: 33px;
  }
  .customer-title {
    font-size: 13px;
    color: #666666;
    padding: 0 15px;
    height: 35px;
    line-height: 35px;
    background: #f5f5f5;
  }
  .tag-data{
    padding-top: 8px;
    padding-bottom: 8px;
    .tag-style {
      display: inline-block;      
      span{
        position: absolute;
        left: 50%;
        top: -30px;
        background: #353535;
        color: #fff;
        border-radius: 5px;
        padding: 3px 0;
        width: 54px;
        margin-left: -27px;
        text-align: center;
        &:after{
          content: '';
          display: block;         
          border-left: 5px transparent dashed;          
          border-right: 5px transparent dashed;
          border-top: 4px #353535 solid;
          border-bottom: 4px transparent dashed;
          position: absolute;
          left: 50%;
          margin-left: -5px;
          top: 25px;
        }
      }
    }
    label{
      display: inline-block;
      border-radius: 50px;
      background: #fff;
      border: #ec6700 1px solid;
      color: #ec6700;
      margin: 6px 14px 6px 0;
      font-size: 12px;
      padding: 2px 18px;
      &.fail{
        border: #999999 1px solid;
        color: #999999;
      }
      &.on{
        border: #ec6700 1px solid;
        color: #ec6700;
      }
      &.active{
        color: #fff;
        background: #ec6700;
        border: #ec6700 1px solid;
      }
    }
    .tag-add{
      display: inline-block;
      vertical-align: middle;
      width: 130px;
      padding: 0 10px!important;
      margin: 6px 0;
      height: 25px!important;
      line-height: 25px!important;
      font-size: 12px!important;      
      &.border-l{
        border-left: #ec6700 2px solid;
      }
    }
  }
  .remark-clear{
    display: block;
    width: 30px;
    height: 30px;
    text-align: center;
    img{
      display: inline-block;
      vertical-align: top;
      margin-top: 8px;
    }
  }
</style>
